package codeGeneration;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import util.HeaderOfJavaFile;

public class Generator {
	protected String[] spaces = new String[8];
	protected int indentIndex;

	protected Generator() {
		for (int i = 0; i < 8; ++i) {
			StringBuffer stemp = new StringBuffer();
			for (int j = 0; j < i; ++j)
				stemp.append('\t');

			spaces[i] = stemp.toString();
		}

		indentIndex = 0;

	}

	protected void indentOneTime(String start, Properties prop, String last,
			StringBuffer sbuffWriter, boolean isEndtag) {
		StringBuffer sbuff = new StringBuffer();
		if (prop != null) {
			Enumeration<Object> keys = prop.keys();

			while (keys.hasMoreElements()) {
				String obj = (String) keys.nextElement();
				String value = prop.getProperty(obj);

				sbuff.append(" " + obj + " = \"" + value + "\"");
			}
		}
		sbuffWriter.append(spaces[indentIndex] + start + sbuff.toString()
				+ last + '\n');

		if (!isEndtag)
			++indentIndex;
	}

	protected void indentOneTime(String start, Properties prop, String last,
			FileWriter fileWriter, boolean isEndtag) throws IOException {
		StringBuffer sbuff = new StringBuffer();
		if (prop != null) {
			Enumeration<Object> keys = prop.keys();

			while (keys.hasMoreElements()) {
				String obj = (String) keys.nextElement();
				String value = prop.getProperty(obj);

				sbuff.append(" " + obj + " = \"" + value + "\"");
			}
		}
		fileWriter.append(spaces[indentIndex] + start + sbuff.toString() + last
				+ '\n');

		if (!isEndtag)
			++indentIndex;
	}

	protected void decreaseIndentOneTime(String name, StringBuffer sbuffWriter) {
		--indentIndex;
		sbuffWriter.append(spaces[indentIndex] + name + '\n');
	}

	protected void decreaseIndentOneTime(String name, FileWriter fileWriter)
			throws IOException {
		--indentIndex;
		fileWriter.append(spaces[indentIndex] + name + '\n');
	}

	protected void appendToFile(StringBuffer sbuffWriter, String s) {
		sbuffWriter.append(spaces[indentIndex] + s);
	}

	protected void appendToFile(FileWriter fileWriter, String s)
			throws IOException {
		fileWriter.append(spaces[indentIndex] + s);
	}

	protected FileWriter getFileWriter(String fileName) throws IOException {
		FileWriter fileWriter = null;
		fileWriter = new FileWriter(fileName);
		return fileWriter;
	}

	protected void closeFileWriter(FileWriter fileWriter) {
		try {
			if (fileWriter != null)
				fileWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void replacePlaceHolder(StringBuffer sbuffWriter,
			HeaderOfJavaFile headerOfJavaFile) {
		replacePackage(sbuffWriter, headerOfJavaFile);
		replaceImport(sbuffWriter, headerOfJavaFile);
	}

	private void replacePackage(StringBuffer sbuffWriter,
			HeaderOfJavaFile headerOfJavaFile) {
		int indexOfPackageName = sbuffWriter
				.indexOf(HeaderOfJavaFile.packageNamePlaceHolder);
		sbuffWriter.replace(indexOfPackageName, indexOfPackageName
				+ HeaderOfJavaFile.packageNamePlaceHolder.length(), "package "
				+ headerOfJavaFile.getPackageName());
	}

	private void replaceImport(StringBuffer sbuffWriter,
			HeaderOfJavaFile headerOfJavaFile) {
		int indexOfImportList = sbuffWriter
				.indexOf(HeaderOfJavaFile.importsListPlaceHolder);
		List<String> importsList = headerOfJavaFile.getImportsList();
		StringBuffer sbuffImportsList = new StringBuffer();
		if (importsList != null && importsList.size() > 0) {
			for (String s : importsList)
				appendToFile(sbuffImportsList, "import " + s + ";\n");
		}

		sbuffWriter.replace(indexOfImportList, indexOfImportList
				+ HeaderOfJavaFile.importsListPlaceHolder.length(),
				sbuffImportsList.toString());
	}
}
